package leo.weixin.service.impl;

import leo.weixin.entity.menu.*;
import leo.weixin.service.IMenuService;
import leo.weixin.service.IWxService;
import leo.weixin.util.RestTemplateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service("iMenuService")
public class MenuServiceImpl implements IMenuService {
    @Autowired
    private IWxService iWxService;

    /**
     * 每个菜单最多5 不能有子菜单的子菜单
     * @return
     */
    @Override
    public String createMenu() {
        String url =  "https://api.weixin.qq.com/cgi-bin/menu/create?access_token=ACCESS_TOKEN";
        url = url.replace("ACCESS_TOKEN",iWxService.getAccessToken());

        //  todo 根据管理后台传来的参数 创建button 对象
        Button button = new Button();
        button.add(EventButton.createClickButton("登录","click_login"))
                .add(new SubButton("主菜单一")
                .add(EventButton.createByNameKeyType("扫码带提示","SCANCODE_WAITMSG", EventButton.EventButtonType.SCANCODE_WAITMSG))
                .add(EventButton.createByNameKeyType("扫码推事件","SCANCODE_PUSH", EventButton.EventButtonType.SCANCODE_PUSH))
                .add(EventButton.createByNameKeyType("图像识别","event_imageRecognition", EventButton.EventButtonType.PIC_PHOTO_OR_ALBUM))
                .add(EventButton.createByNameKeyType("微信相册发图","PIC_WEIXIN", EventButton.EventButtonType.PIC_WEIXIN))
                .add(EventButton.createByNameKeyType("系统拍照发图","PIC_SYSPHOTO", EventButton.EventButtonType.PIC_SYSPHOTO)))
                .add(new SubButton("主菜单二")
                        .add(EventButton.createLocationButton("发送位置","click_location"))
                        .add(new ViewButton("QQ音乐2","https://y.qq.com/"))
                        .add(new ViewButton("打开小程序","https://y.qq.com/")) //todo  修改为小程序
                        .add(EventButton.createClickButton("支持我","click_support")));
                                //.add(MediaButton.createNewsMediaButton("图文消息按钮","111"))
                                //.add(MediaButton.createTextMediaButton("文本消息按钮","222"))));
        String result = RestTemplateUtil.post(url,button);
        System.out.println(result);
        return result;
    }

    @Override
    public String getMenuInfo() {
        String url =  "https://api.weixin.qq.com/cgi-bin/get_current_selfmenu_info?access_token=ACCESS_TOKEN";
        url = url.replace("ACCESS_TOKEN",iWxService.getAccessToken());
        String result = RestTemplateUtil.get(url);
        System.out.println(result);
        return null;
    }

    @Override
    public String delMenu() {
        String url =  "https://api.weixin.qq.com/cgi-bin/menu/delete?access_token=ACCESS_TOKEN";
        url = url.replace("ACCESS_TOKEN",iWxService.getAccessToken());
        String result = RestTemplateUtil.get(url);
        System.out.println(result);
        return null;
    }

}
